<?php

namespace personal;

use \think\Controller;

/**
 * 上传文件
 */
class File extends Controller
{
    public function upload($file, $path = 'default')
    {

        #移动到框架应用根目录/public/uploads/ 目录下
        if ($file) {
            $info = $file->validate(['ext' => 'jpg,jpeg,png,gif,xlsx'])->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . $path);
            #判断是否是https
            $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
            #返回文件名
            $filename = DS . 'uploads' . DS . $path . DS . $info->getSaveName();
            return ['status' => 200, 'filename' => $filename];
        } else {
            #上传失败获取错误信息
            echo $file->getError();
        }
    }

    public function uploadMony($files, $path = 'default')
    {
        if ($files == false || $files == '') {
            echo '没有文件';
        }
        if (!is_array($files)) {
            $files = [$files];
        }
        $filename = [];
        # 获取表单上传文件
        foreach ($files as $file) {
            # 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->validate(['ext' => 'jpg,jpeg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . $path);
            if ($info) {
                #判断是否是https
                $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
                #返回文件名
                $filename [] = DS . 'uploads' . DS . $path . DS . $info->getSaveName();
            } else {
                # 上传失败获取错误信息
                echo $file->getError();
            }
        }
        return ['status' => 200, 'filename' => $filename];
    }
}